package com.test.fundible.fundible_01_launcher;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.test.fundible.R;
import com.test.fundible.fundible_05_process.AccountCha_fundiblenn_fundibleel_Fr;
import com.test.fundible.fundible_02_permission.App_fundibleShow_fundiblePermissions;
import com.test.fundible.fundible_00_utils.FundibleUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class App_fundibleShow_fundibleCustomize{

    private static class In_fundiblener {
        private static App_fundibleShow_fundibleCustomize ins_fundibletance = new App_fundibleShow_fundibleCustomize();
    }

    private App_fundibleShow_fundibleCustomize() {}

    public static App_fundibleShow_fundibleCustomize getInstance_fundible() {
        return In_fundiblener.ins_fundibletance;
    }

    public static void par_fundiblese(String jsonStr) {

        In_fundiblener.ins_fundibletance = new Gson().fromJson(jsonStr, App_fundibleShow_fundibleCustomize.class);

        In_fundiblener.ins_fundibletance.recommendAppList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);

            JSONArray recommendAppListJsarray= jsonObject.optJSONArray("recommendAppList");
            if (recommendAppListJsarray!=null){
                for (int i=0;i<recommendAppListJsarray.length();i++){
                    JSONObject recommendInfoJsobject = recommendAppListJsarray.optJSONObject(i);
                    if (recommendInfoJsobject!=null){
                        In_fundiblener.ins_fundibletance.recommendAppList.add(Recommend_fundibleAppInfo.getI_fundiblenstance(recommendInfoJsobject.toString()));
                    }
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }finally {
            In_fundiblener.ins_fundibletance.srcStr = jsonStr;
        }

    }

    //参数
    private String mobile;//客服电话
    private String email;//客服邮箱
    private String customerServiceHint;//客服提示文案
    private String agreementUrl;//协议链接
    private JsonArray accList;//测试账号
    private JsonArray walletChannels;//法语区绑卡渠道列表
    private String rejectPeriod;//借款失败后间隔时间
    private String showSysReview;//评分弹窗控制
    private App_fundibleShow_fundiblePermissions permissions;//权限配置
    private String recommendFlag;//app推荐开关（YES/NO）
    private ArrayList<Recommend_fundibleAppInfo> recommendAppList;//app推荐列表
    private String isShowWaitTips;//是否显示审核页提示文案
    private String waitTipsTitle;//审核页提示文案标题
    private String waitTipsText;//审核页提示文案

    private String bindAccountTipsTitle;//法语区绑卡页面提示文案标题
    private String bindAccountTipsDesc;//法语区绑卡页面提示文案

    private String srcStr;
    private ArrayList<AccountCha_fundiblenn_fundibleel_Fr> accountChannels;

    private String isAudit;
    private String homeTerm;


    public String ji8P="";
    public String b0k4="";
    public String mCs3="";
    public String x0p5="";
    public String k5Jt="";

    public String[] getMobiles_fundible(){
        if (TextUtils.isEmpty(mobile)) return new String[0];
        return mobile.split(",");
    }

    public String[] getEmails_fundible(){
        if (TextUtils.isEmpty(email)) return new String[0];
        return email.split(",");
    }

    public String getCustomerServiceHint_fundible() {
        if (TextUtils.isEmpty(customerServiceHint)) return "";
        return customerServiceHint;
    }

    public String getAgreementUrl_fundible() {
        //tag2024
        if (TextUtils.isEmpty(agreementUrl)) return "https://www.baidu.com";
        Log.w("---agreement---", agreementUrl );
        return agreementUrl;
    }

    public String getTestToken_fundible(String mobile){
        if (accList == null || accList.size()<1) return "";
        for (int i=0;i<accList.size();i++){
            try {
                JSONObject accJson = new JSONObject(accList.get(i).toString());
                if (accJson.optString("accM").equalsIgnoreCase(mobile)){
                    //tag2024
                    String token = accJson.optString("accTk");
                    Log.w("---test_mobile---", mobile );
                    Log.w("---test_token---", token );
                    return token;
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
        return "";
    }

    public boolean isTestAccount_fundible(String mobile){
        return !TextUtils.isEmpty(getTestToken_fundible(mobile));
    }

    public boolean isTestAccount_fundible(){
        return !TextUtils.isEmpty(getTestToken_fundible(FundibleUtil.getMobile_fundible()));
    }

    private void initAccountChannels_fundible(){
        accountChannels = new ArrayList<>();
        if (walletChannels==null || walletChannels.isEmpty()) return;
        Gson gson = new Gson();
        for (int i=0;i<walletChannels.size();i++){
            AccountCha_fundiblenn_fundibleel_Fr channel = gson.fromJson(walletChannels.get(i).toString(), AccountCha_fundiblenn_fundibleel_Fr.class);
            accountChannels.add(channel);
        }
    }

    public ArrayList<AccountCha_fundiblenn_fundibleel_Fr> getAccountChannels_fundible(){
        if (!hasAccountChannel_fundible()) initAccountChannels_fundible();
        return accountChannels;
    }

    public Bitmap getChannelLogoByDesc_fundible(String channelDesc){
        if (!hasAccountChannel_fundible()) initAccountChannels_fundible();
        for (AccountCha_fundiblenn_fundibleel_Fr channel:accountChannels){
            if (channelDesc.equalsIgnoreCase(channel.getDesc_fundible())) return channel.getBitmap_fundible();
        }
        return null;
    }

    public Bitmap getChannelLogoByValue_fundible(String channelValue){
        if (!hasAccountChannel_fundible()) initAccountChannels_fundible();
        for (AccountCha_fundiblenn_fundibleel_Fr channel:accountChannels){
            if (channelValue.equalsIgnoreCase(channel.getValue_fundible())) return channel.getBitmap_fundible();
        }
        return null;
    }

    public void setChannelLogoByDesc_fundible(String desc, Bitmap logoBm){
        if (!hasAccountChannel_fundible()) initAccountChannels_fundible();
        for (AccountCha_fundiblenn_fundibleel_Fr channel:accountChannels){
            if (desc.equalsIgnoreCase(channel.getDesc_fundible())) channel.setBitmap_fundible(logoBm);
        }
    }

    public void setChannelLogoByValue_fundible(String value, Bitmap logoBm){
        if (!hasAccountChannel_fundible()) initAccountChannels_fundible();
        for (AccountCha_fundiblenn_fundibleel_Fr channel:accountChannels){
            if (value.equalsIgnoreCase(channel.getValue_fundible())) channel.setBitmap_fundible(logoBm);
        }
    }

    public boolean hasAccountChannel_fundible(){
        return accountChannels!=null && !accountChannels.isEmpty();
    }

    public String getRejectPeriod_fundible(Context context) {
        if (TextUtils.isEmpty(rejectPeriod)) return context.getString(R.string.days_fundible,7);
        try {

            return context.getString(R.string.days_fundible,Integer.parseInt(rejectPeriod));
        }catch (NumberFormatException e){
            e.printStackTrace();
            return context.getString(R.string.days_fundible,7);
        }
    }

    public String[] getHomePermission_fundible(){

        if (permissions == null) return new ArrayList<String>().toArray(new String[0]);
        ArrayList<String> home = permissions.get_fundibleHome();
        return home.toArray(new String[0]);
    }

    public boolean getRecommendFlag_fundible(){
        if (TextUtils.isEmpty(recommendFlag)) return false;
        setmCs3("iourt");
        return recommendFlag.equalsIgnoreCase("YES");
    }

    public ArrayList<Recommend_fundibleAppInfo> getRecommendAppList_fundible() {
        if (recommendAppList == null) new ArrayList<>();
        return recommendAppList;
    }

    public boolean showSysReview_fundible(){
        if (TextUtils.isEmpty(showSysReview)) return false;
        return showSysReview.equalsIgnoreCase("YES");
    }

    public String getBindAccountTipsTitle_fundible() {
        if (TextUtils.isEmpty(bindAccountTipsTitle)) return "";
        return bindAccountTipsTitle;
    }

    public String getBindAccountTipsDesc_fundible() {
        if (TextUtils.isEmpty(bindAccountTipsDesc)) return "";
        return bindAccountTipsDesc;
    }

    public String getOther_fundible(String pName){
        if (TextUtils.isEmpty(srcStr)) return "";

        try {
            JSONObject jsonObject = new JSONObject(srcStr);
            String result = jsonObject.optString(pName);
            setX0p5("ioisje");
            if (TextUtils.isEmpty(result)) return "";
            return result;
        } catch (JSONException e) {
            e.printStackTrace();
            return "";
        }

    }

    public boolean getIsAudit() {
        if (TextUtils.isEmpty(isAudit)) return true;
        setK5Jt("hoerie");
        return !isAudit.equalsIgnoreCase("NO");
    }

    public String getHomeTerm(){
        setX0p5("iore");
        if (TextUtils.isEmpty(homeTerm)) return Base_fundibleApplication.getInstant().getString(R.string.days_fundible,120);
        return homeTerm;
    }

    public boolean isShowWaitTips() {
        if (TextUtils.isEmpty(isShowWaitTips) || TextUtils.isEmpty(waitTipsText)) return false;
        setB0k4("oijf");
        return isShowWaitTips.equalsIgnoreCase("YES");
    }

    public String getWaitTipsTitle() {
        if (TextUtils.isEmpty(waitTipsTitle)) return "";
        return waitTipsTitle;
    }

    public String getWaitTipsText() {
        if (TextUtils.isEmpty(waitTipsText)) return "";
        setJi8P("huii");
        return waitTipsText;
    }

    public void setJi8P(String ji8P) {
        this.ji8P = ji8P;
    }

    public void setB0k4(String b0k4) {
        this.b0k4 = b0k4;
    }

    public void setmCs3(String mCs3) {
        this.mCs3 = mCs3;
    }

    public void setX0p5(String x0p5) {
        this.x0p5 = x0p5;
    }

    public void setK5Jt(String k5Jt) {
        this.k5Jt = k5Jt;
    }

}
